package jdbc;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * Description:初学习
 * User:22954
 * Date:2024-05-10
 * Time:18:05
 */
public class JDBC01 {
    /*
    使用JDBC流程
    1.配置数据库的驱动: 在项目创建lib文件夹,将mysql.jar拷贝至lib下,add as library
    2.注册驱动 Driver----com.mysql.cj.jdbc.Driver
    3.建立连接 使用驱动器连接
    4.执行sql语句  Statement--用于执行静态SQL语句并返回生成结果
    5.关闭连接
     */
    public static void main(String[] args) throws SQLException {
        Driver driver = new Driver();

        //url = jdbc:mysql://ip:port/database
        String url = "jdbc:mysql://localhost:3306/java_db01";
        Properties info = new Properties();//通过配置文件输入登录用户及密码
        info.setProperty("user", "root");
        info.setProperty("password", "javamysql");
        Connection connection = driver.connect(url, info);

        String sql = "insert into student values (null,'mary')";
        sql = "update student set `name` = 'kai' where `no` = 1;";
        Statement statement = connection.createStatement();
        int affectedRows = statement.executeUpdate(sql);

        System.out.println(affectedRows > 0 ? "Success" : "Fail");

        statement.close();
        connection.close();
    }
}
